/*
search(value, searchfield, resultcontainer, callback: result show/hide, callback: valid true/false)

callbacks:

result show/hide: arg1 = display container for results
valid true/false: arg1 = input is valid bandname (do whatever you wanna do)
*/

 var search_cb_valid = "", search_cb_resshow = "";
 var search_fn_result = null, search_fn_input = null;
 var sct;

 function search(typ, q, id, result, resshow, valid) {
  window.clearTimeout(sct);
  search_cb_valid = valid;
  search_cb_resshow = resshow;
  search_fn_result = document.getElementById(result);
  search_fn_input = document.getElementById(id);
  set_callback("search_done");
  phpget("search_backend.php", "q="+escape(q)+"&f="+escape(id)+"&typ="+escape(typ), result);
  search_fn_input.focus();
 }
 
 function search_done() {
  mel = document.getElementById("search_matches");
  // matches
  matches = 0;
  if(mel) {
   matches = mel.value;
   if(matches > 0) {
    // we have some matches
    window[search_cb_resshow](1);
    // is it already a valid input?
    if(parseInt(document.getElementById("search_totmatch").value) == 1) {
     window[search_cb_valid](1);
    } else {
     window[search_cb_valid](0);
    }
    /*
    // auto take?
    if(matches == 1) {
     set("bandname", document.getElementById("search_bandleft").value);
    }
    */
   }
  }
  if(!mel || matches == 0) {
   window[search_cb_resshow](0);
   window[search_cb_valid](0);
  }
 }
 
 function set(field, _name) {
  document.getElementById(field).value = _name;
  window[search_cb_resshow](0);
  window[search_cb_valid](1);
 }
 
 function search_get_input() {
  return search_fn_input;
 }
 
 function search_get_resultcontainer() {
  return search_fn_result;
 }
 
 function search_cancel() {
  sct = window.setTimeout("search_canceled()", 500);
 }
 
 function search_canceled() {
  window[search_cb_resshow](0);
 }